﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Windows;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Interaction logic for SelectSongsWindow.xaml
    /// </summary>
    public partial class SelectSongsWindow : Window
        {

        public Song[] SelectedSongs { get; private set; }

        public SelectSongsWindow()
            {
            InitializeComponent();
            var songs = State.Database.Songs.GetAllSongs().OrderBy(song => song.Title);
            var items = CheckListBoxItem.CreateItems(songs.ToArray());
            SongsListBox.ItemsSource = items;
            }

        public SelectSongsWindow(Song[] availableSongs, Song[] excludedSongs)
            {
            InitializeComponent();
            var sortedSongs = availableSongs.OrderBy(song => song.Title);
            foreach (var song in sortedSongs)
                {
                if (Array.IndexOf<Song>(excludedSongs, song) == -1)
                    {
                    var item = new CheckListBoxItem(song);
                    SongsListBox.Items.Add(item);
                    }
                }
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            // Populate the selected songs
            var songs = new List<Song>();
            foreach (CheckListBoxItem item in SongsListBox.Items)
                {
                if (item.IsChecked) songs.Add((Song)item.Value);
                }
            this.SelectedSongs = songs.ToArray();

            // Close the window
            this.DialogResult = true;

            }

        }

    }
